<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/1/23
 * Time: 9:57
 * 首页相关控制器
 */
namespace app\index\controller;
use app\admin\controller\Common;
use app\index\model\Role;
use think\Db;
use think\Exception;
use think\Session;

class Index extends Base
{

    /**
     * 样式切换
     * @author zhixuedong
     * @date 2021/1/25/025
     */
    public function switchStyle(){
        try{
            $id = Session::get("id");
            $data{'style'} = $_POST['style'];
            Db::name("account")->where(['id'=>$id])->update($data);
            Session::set("style", $_POST['style']);
            $break['code'] = 0;
            $break['msg'] = "操作成功";
        }catch (Exception $e){
            $break['code'] = 202;
            $break['msg'] = $e->getMessage();
        }
        return $break;

    }

    /**
     * 首页html展示
     * @return mixed
     */
    public function index()
    {
        $role = Session::get("role");
        $account = new \app\index\model\Account();
        $account->getRoles($role);



        //获取平台标题
        $platform = Db::name("config_platform")->find();
        $this->assign("platform",$platform);
        $sub=Session::get("sub");
        $temp=Session::get("temp");
        $user = Session::get("user_name");
        $this->assign("user",$user);
        $auto = Session::get("auto");
        if($auto == 4){
            $store_id = Db::name("system_stores_manage")->where(['id'=>Session::get('id')])->value("store_id");
            //获取商户号权限
            $service = Db::name("system_stores")->where(['id'=>$store_id])->value("service");
            $service = explode(",",$service);
            foreach ($sub as $k=>$v){
                if($v['id']==139){
                    foreach ($v["sub"] as $k1=>$v1){
                        if(!in_array($v1['id'],$service)){
                            unset($v["sub"][$k1]);
                        }
                    }
                    $sub[$k]['sub'] = $v["sub"];
                }
            }
        }
        $uniacid = Session::get("uniacid");
        $configWhere = [];
        //$configWhere['status'] = 1;
        if(!empty($uniacid)){
            $configWhere['id'] = $uniacid;
        }
        //获取公众号列表
        $thisWechat = Session::get("thisWechat");
        $wechat = Db::name("config")->where($configWhere)->select();
        foreach ($wechat as $k=>$v){
            $wechat[$k]['avatar'] = empty($v['avatar'])?"/static/images/nopic-107.png":$v['avatar'];
        }
        $this->assign("wechat",$wechat);

        //当前公众号
        if(empty($thisWechat)){
            Session::set("thisWechat",$wechat[0]);
            $thisWechat = $wechat[0];
        }
        $this->assign("thisWechat",$thisWechat);
        return $this->fetch("index",["sub"=>$sub,"temp"=>$temp]);
    }
    
    public function index_show()
    {
        $user = Session::get("user_name");
        $this->assign("user",$user);
        $thisWechat = Session::get("thisWechat");
        $uniacid = $thisWechat['id'];


        $where['status'] = 1;
        $where['uniacid'] = $uniacid;
        $investment = Db::name("project_pro")->where($where)->sum("investment");
        $investment = $investment;

        //本年度计划投资
        $where1['b.status'] = 1;
        $where1['b.uniacid'] = $uniacid;
        $plan_investment = Db::name("project_pro_plan")->alias('a')
            ->join("project_pro b","a.pro_id = b.id")->where($where1)->order("a.id desc")->sum("plan_investment");
        $plan_investment = $plan_investment;

        $this->assign("investment",$investment);
        $this->assign("plan_investment",$plan_investment);


        $procount = Db::name("project_pro")->where($where)->count("*");
        $this->assign("procount",$procount);


        //查询字典
        $pro_type = Db::name("project_config")->where(['uniacid'=>$uniacid])->value("pro_type");
        $pro_type = explode("|","$pro_type");
        $data = [];
        $count = [];
        $harmonious = 0;
        foreach ($pro_type as $k=>$v){
            $v = explode(":","$v");
            $data[] = $v[1];
            $procount1 = Db::name("project_pro")->where(['project_type'=>$v[0]])->count("*");
            $count[] = $procount1;
            //统计项目类别
            $harmonious = $harmonious+$procount1;
        }
        $other = $procount - $harmonious;
        $data[] = "其他";
        $count[] = $other;

        $this->assign("data",json_encode($data));
        $this->assign("count",json_encode($count));


        return $this->fetch();
    }

    /**
     * 前端点击
     * @author zhixuedong
     * @date 2021/2/4/004
     */
    public function webClick(){
        try{
            //查询票类型
            $tickets = Db::name("tickets_tickets")->where(['id'=>['gt',0]])->field("id,title")->select();
            $newData = date("Y-m-d",strtotime($_POST['data']));
            $thatday = $this->thatday($tickets,$newData);
            $thatweek = $this->thatweek($tickets,$newData);
            $thatmonth = $this->thatmonth($tickets,$newData);

            $thatdayHtml = "";
            foreach ($thatday as $k=>$v){
                $thatdayHtml .= "<tr><td>".$v['title']."</td><td>".$v['total']."</td></tr>";
            }

            $thatweekHtml = "";
            foreach ($thatweek as $k=>$v){
                $thatweekHtml .= "<tr><td>".$v['title']."</td><td>".$v['total']."</td></tr>";
            }

            $thatmonthHtml = "";
            foreach ($thatday as $k=>$v){
                $thatmonthHtml .= "<tr><td>".$v['title']."</td><td>".$v['total']."</td></tr>";
            }

            $black['thatdayHtml'] = $thatdayHtml;
            $black['thatweekHtml'] = $thatweekHtml;
            $black['thatmonthHtml'] = $thatmonthHtml;
            $black['code'] = 0;

        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }


    public function get_weekinfo_by_date($date) {
        $idx = strftime("%u", strtotime($date));
        $mon_idx = $idx - 1;
        $sun_idx = $idx - 7;
        return array(
            'week_start_day' => strftime('%Y-%m-%d', strtotime($date) - $mon_idx * 86400),
            'week_end_day' => strftime('%Y-%m-%d', strtotime($date) - $sun_idx * 86400),
        );
    }

    //当天
    public function thatday($tickets,$newData){
        foreach ($tickets as $k=>$v){
            //查询已售总票
            $totalWhere['tickets_id'] = $v['id'];
            $totalWhere['create_data'] = $newData;
            $newtotal = Db::name("tickets_list")->where($totalWhere)->count("*");
            $tickets[$k]['total'] = empty($newtotal)?0:$newtotal;
        }
        return $tickets;
    }

    //本周
    public function thatweek($tickets,$newData){
        $value = $this->get_weekinfo_by_date($newData);
        $start = date("Y-m-d",strtotime($value['week_start_day']));
        $end = date("Y-m-d",strtotime($value['week_end_day']));
        foreach ($tickets as $k=>$v){
            //查询已售总票
            $totalWhere['tickets_id'] = $v['id'];
            $totalWhere['create_data'] = ['between',[$start,$end]];
            $newtotal = Db::name("tickets_list")->where($totalWhere)->count("*");
            $tickets[$k]['total'] = empty($newtotal)?0:$newtotal;
        }
        return $tickets;
    }

    //本月
    public function thatmonth($tickets,$newData){
        $newData = date("Y-m",strtotime($newData));
        foreach ($tickets as $k=>$v){
            //查询已售总票
            $totalWhere['tickets_id'] = $v['id'];
            $totalWhere['create_data'] = ['like',"%".$newData."%"];
            $newtotal = Db::name("tickets_list")->where($totalWhere)->count("*");
            $tickets[$k]['total'] = empty($newtotal)?0:$newtotal;
        }
        return $tickets;
    }



    public function showService(){
        //当前sql版本
        $mysqlinfo = Db::query("select VERSION()");
        $MySQL = $mysqlinfo[0]['VERSION()'];
        $this->assign("MySQL",$MySQL);

        //浏览器类型
        $explorer = $this->getBrowser();
        $this->assign("explorer",$explorer);

        $ip=$this->getUserIpAddr();
        $this->assign("ip",$ip);

        //当前时间
        $nowtime = date("Y年m月d日");
        $this->assign("nowtime",$nowtime);
    }


    function getUserIpAddr(){
        if(!empty($_SERVER['HTTP_CLIENT_IP'])){
            //ip from share internet
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        }elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
            //ip pass from proxy
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        }else{
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }

    function getBrowser() {
        $user_OSagent = $_SERVER['HTTP_USER_AGENT'];
        if (strpos($user_OSagent, "Maxthon") && strpos($user_OSagent, "MSIE")) {
            $visitor_browser = "Maxthon(Microsoft IE)";
        } elseif (strpos($user_OSagent, "Maxthon 2.0")) {
            $visitor_browser = "Maxthon 2.0";
        } elseif (strpos($user_OSagent, "Maxthon")) {
            $visitor_browser = "Maxthon";
        } elseif (strpos($user_OSagent, "Edge")) {
            $visitor_browser = "Edge";
        } elseif (strpos($user_OSagent, "Trident")) {
            $visitor_browser = "IE";
        } elseif (strpos($user_OSagent, "MSIE")) {
            $visitor_browser = "IE";
        } elseif (strpos($user_OSagent, "MSIE")) {
            $visitor_browser = "MSIE 较高版本";
        } elseif (strpos($user_OSagent, "NetCaptor")) {
            $visitor_browser = "NetCaptor";
        } elseif (strpos($user_OSagent, "Netscape")) {
            $visitor_browser = "Netscape";
        } elseif (strpos($user_OSagent, "Chrome")) {
            $visitor_browser = "Chrome";
        } elseif (strpos($user_OSagent, "Lynx")) {
            $visitor_browser = "Lynx";
        } elseif (strpos($user_OSagent, "Opera")) {
            $visitor_browser = "Opera";
        } elseif (strpos($user_OSagent, "MicroMessenger")) {
            $visitor_browser = "微信浏览器";
        } elseif (strpos($user_OSagent, "Konqueror")) {
            $visitor_browser = "Konqueror";
        } elseif (strpos($user_OSagent, "Mozilla/5.0")) {
            $visitor_browser = "Mozilla";
        } elseif (strpos($user_OSagent, "Firefox")) {
            $visitor_browser = "Firefox";
        } elseif (strpos($user_OSagent, "U")) {
            $visitor_browser = "Firefox";
        } else {
            $visitor_browser = "其它";
        }
        return $visitor_browser;
    }

}
